package com.mt.sdk.ble.model;

import com.mt.sdk.tools.MTTools;
import android.bluetooth.BluetoothGattCharacteristic;

public class SendBigDatasAction extends BLEBaseAction {

	private byte[] datas;
	private BluetoothGattCharacteristic sendCharact;
	private BluetoothGattCharacteristic reviceCharact;

	public SendBigDatasAction(byte[] datas, BluetoothGattCharacteristic sendCharact,
			BluetoothGattCharacteristic reviceCharact, Option option) {
		super(ActionType.SENDBIGDATAS, option);
		this.datas = datas;
		this.sendCharact = sendCharact;
		this.reviceCharact = reviceCharact;

		checkDatas = 0;
		for (byte b : datas) {
			checkDatas += b;
		}

		blocks = datas.length / (option.getPerlenght()); // 计算一共有多少块
		if (datas.length % (option.getPerlenght()) != 0) {
			blocks++;
		}
	}

	// 提取分段后的数据
	public byte[] getDatas() {
		int point = blocking*option.getPerlenght();
		if ((datas == null) || (point >= datas.length)) {
			System.out.println("point->"+point+"    datas.length->"+datas.length);
			return null;
		}
		byte getbyte[];
		if ((option.getPerlenght() + point) > datas.length) {
			getbyte = new byte[datas.length - point];
			MTTools.arraycopy(datas, point, getbyte, 0, getbyte.length);
		} else {
			getbyte = new byte[option.getPerlenght()];
			MTTools.arraycopy(datas, point, getbyte, 0, getbyte.length);
		}
		blocking++;
//		point += getbyte.length;
		return getbyte;
	}

	// 获取校验码
	private byte checkDatas = 0;
	public byte getCheckDatas() {
		return checkDatas;
	}
	
	// 获取校验码
	public static byte getCheckDatasFromBytes(byte[] datas){
		
		if(datas == null){
			return 0;
		}
		
		byte checkDatas = 0;
		for (byte b : datas) {
			checkDatas += b;
		}
		return checkDatas;
	}

	// 获取块大小
	private int blocks = 0;
	private int blocking = 0;
	public int getBlocks() {
		return blocks;
	}

	// 获取正在发送的指针
	public int getSendingIndex() {
		return blocking;
	}

	public void onSendProcess(int blockIndex) {

	}

	public BluetoothGattCharacteristic getSendCharact() {
		return sendCharact;
	}

	public void setSenddatasCharact(BluetoothGattCharacteristic sendCharact) {
		this.sendCharact = sendCharact;
	}

	public BluetoothGattCharacteristic getReviceCharact() {
		return reviceCharact;
	}

	public void setRevicedatasCharact(BluetoothGattCharacteristic reviceCharact) {
		this.reviceCharact = reviceCharact;
	}

}
